package cn.com.dashihui.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.SMS;
import cn.com.dashihui.web.service.SMSService;

@RequiresAuthentication
public class SMSController extends BaseController{
	private SMSService service = new SMSService();
    
    public void index(){
        render("index.jsp");
    }
    
    public void doSort(){
    	Map<String,String[]> params = getParaMap();
    	if(params!=null&&params.size()!=0&&params.containsKey("sortKey")){
    		String sortKey = params.get("sortKey")[0];
    		Map<String,String> sortMap = new HashMap<String,String>();
    		for(String key : params.keySet()){
    			if(!key.equals("sortKey")){
    				String id = key.replace(sortKey, "");
    				String no = params.get(key)[0];
    				if(StrKit.isBlank(no)||no.length()>3){
    					no = "0";
    				}
    				sortMap.put(id, no);
    			}
    		}
    		service.sortConfig(sortMap);
    		renderSuccess();
    		return;
    	}
    	renderFailed();
    }

	public void page(){
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		renderResult(0,service.findByPage(pageNum, pageSize,userid,RoleId));
	}
    
	public void toAdd(){
		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		if(RoleId==1){
			
		}else if(RoleId==2){
			Record record=service.findSellerIdByUserId(userid);
			setAttr("record", record);
		}else if(RoleId==3){
			Record record=service.findStoreIdByUserId(userid);
			setAttr("record", record);
		}
		setAttr("RoleId", RoleId);
		render("add.jsp");
	}
	
	/**
	 * 添加
	 * @return -1：异常，0：成功
	 */
	public void doAdd(){
		//代码
		String aname = getPara("aname");
		//值
		String phone = getPara("phone");
		//描述
		String type = getPara("type");
		int RoleId=getParaToInt("RoleId");
		int sellerid=0;
		int storeid=0;
		if(RoleId==2){
			 sellerid=getParaToInt("sellerid");
		}else if(RoleId==3){
			sellerid=getParaToInt("sellerids");
			storeid=getParaToInt("storeid");
		}
		if(StrKit.isBlank(aname)){
			renderResult(1);
			return;
		}else if(service.findByPhone(phone)!=null){
			renderResult(3);
			return;
		}else if(StrKit.isBlank(phone)){
			renderResult(4);
			return;
		}else{
			//保存
			SMS sms = new SMS()
				.set("name", aname)
				.set("phone", phone)
				.set("type", type)
				.set("sellerid", sellerid)
				.set("storeid", storeid)
				.set("date", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));
			if(service.addSMS(sms)){
				renderSuccess(service.findById(sms.getInt("id")));
				return;
			}
		}
		renderFailed();
	}
	
	public void toEdit(){
		int id = getParaToInt(0,0);
		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		if(RoleId==1){
			
		}else if(RoleId==2){
			Record record=service.findSellerIdByUserId(userid);
			setAttr("record", record);
		}else if(RoleId==3){
			Record record=service.findStoreIdByUserId(userid);
			setAttr("record", record);
		}
		if(id!=0){
			setAttr("object", service.findById(id));
		}
		setAttr("RoleId", RoleId);
		render("edit.jsp");
	}
	
	/**
	 * 更新
	 * @return -1：异常，0：成功，1：值为空，2：值过长，3：描述过长
	 */
	public void doEdit(){
		//id
		String smsid = getPara("smsid");
		//代码
		String aname = getPara("aname");
		//值
		String phone = getPara("phone");
		//描述
		String type = getPara("type");
		int RoleId=getParaToInt("RoleId");
		int sellerid=0;
		int storeid=0;
		if(RoleId==2){
			 sellerid=getParaToInt("sellerid");
		}else if(RoleId==3){
			sellerid=getParaToInt("sellerids");
			storeid=getParaToInt("storeid");
		}
		if(StrKit.isBlank(smsid)){
			renderFailed();
			return;
		}else if(StrKit.isBlank(phone)){
			renderResult(1);
			return;
		}else{
			//更新
			SMS sms = new SMS()
				.set("id", smsid)
				.set("name", aname)
				.set("phone", phone)
				.set("type", type)
				.set("sellerid", sellerid)
				.set("storeid", storeid)
				.set("date", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));
			if(service.editConfig(sms)){
				renderSuccess(service.findById(Integer.valueOf(smsid)));
				return;
			}
		}
		renderFailed();
	}
	
	/**
	 * 删除
	 * @return -1：删除失败，0：删除成功
	 */
	public void doDelete(){
		int id = getParaToInt(0,0);
		if(id!=0&&service.delConfig(id)){
			renderSuccess();
			return;
		}renderFailed();
	}
}
